<template>
  <div class="app-container situation">
    <!--表单-->

    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-select v-model="searchObj.type" clearable placeholder="请选择">
          <el-option label="全部数据统计" value="" />
          <el-option label="学员登录数统计" value="login_num" />

          <el-option label="学员注册数统计" value="register_num" />

          <el-option label="课程播放数统计" value="video_view_num" />

          <el-option label="每日课程数统计" value="course_num" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="searchObj.start"
          type="date"
          placeholder="选择开始日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="searchObj.end"
          type="date"
          placeholder="选择截止日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <el-button
        :disabled="btnDisabled"
        type="primary"
        icon="el-icon-search"
        @click="showChart()"
        >查询</el-button
      >
    </el-form>
    <div class="chart-container">
      <div id="chart" class="chart" style="height: 750px; width: 100%" />
    </div>
  </div>
</template>

<script>
// import echarts from "echarts";
const echarts = require("echarts/lib/echarts");
require("echarts/lib/component/title");
require("echarts/lib/component/toolbox");
require("echarts/lib/component/tooltip");
require("echarts/lib/component/grid");
require("echarts/lib/component/legend");
require("echarts/lib/chart/line");

import staApi from "@/api/sta/sta.js";
export default {
  name: "",
  data() {
    return {
      searchObj: {
        type: "",
        start: "",
        end: "",
      },
      btnDisabled: false,
      chart: null,
      title: "",

      totalData: {},
    };
  },
  created() {
    this.showChart();
  },
  methods: {
    //用于获取数据并且显示
    showChart() {
      staApi.getData(this.searchObj).then((response) => {
        this.totalData = response.data;
        this.setChart();
      });
    },
    setChart() {
      var myChart = echarts.init(document.getElementById("chart"));

      var option = {
        title: {
          text: "数据分析",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["courseNum", "loginNum", "registerNum", "videoViewNum"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },

        xAxis: {
          type: "category",
          // boundaryGap: true,
          data: this.totalData.xData,
          name: "时间线",
        },
        yAxis: {
          type: "value",
          name: "人数",
        },
        series: [
          {
            name: "courseNum",
            type: "line",
            data:
              this.totalData.courseNum == null ? [] : this.totalData.courseNum,
          },
          {
            name: "loginNum",
            type: "line",
            data:
              this.totalData.loginNum == null ? [] : this.totalData.loginNum,
          },
          {
            name: "registerNum",
            type: "line",
            data:
              this.totalData.registerNum == null
                ? []
                : this.totalData.registerNum,
          },
          {
            name: "videoViewNum",
            type: "line",
            data:
              this.totalData.videoViewNum == null
                ? []
                : this.totalData.videoViewNum,
          },
        ],
      };

      myChart.setOption(option);
    },
  },
};
</script>

<style scoped>
.situation {
  width: 95%;
  margin: 0 auto;
  text-align: center;
  margin-top: 40px;
}
</style>